using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FitnessMVC.Data;

namespace FitnessMVCWeb.Controllers
{
    public class BlogController : Controller
    {
        public class MiniPost
        {
            public int PostId { get; private set; }
            public string MiniContents { get; private set; }
            public string Title { get; private set; }

            public MiniPost()
                : this(-1, "", "")
            {

            }

            public MiniPost(BlogPost bp)
                : this(bp.PostId, bp.Contents, bp.Title)
            {

            }

            public MiniPost(int postId, string contents, string title)
            {
                if (contents.Length < 250)
                    MiniContents = contents;
                else
                    MiniContents = contents.Substring(0, 247) + "...";
                PostId = postId;
                Title = title;
            }
        }

        public class BlogViewModel
        {
            public Blog blog { get; private set; }
            public List<BlogPost> posts { get; private set; }
            public List<MiniPost> miniPosts { get; private set; }

            public BlogViewModel()
                : this(new Blog(), new List<BlogPost>())
            {

            }

            public BlogViewModel(Blog b)
                : this(b, b.BlogPosts.ToList())
            {

            }

            public BlogViewModel(Blog b, List<BlogPost> bps)
            {
                blog = b;
                posts = bps;
                miniPosts = new List<MiniPost>();
                
                // TODO: Could possibly switch this to lazy load in the getter.
                bps.ForEach(p => miniPosts.Add(new MiniPost(p)));
            }

               
        }

        IFitnessRepository fitnessRepository;

        // Pattern from NerdDinner
        // (Poor Mans) Dependency Injection enabled constructors

        public BlogController()
            : this(new SqlFitnessRepository()) {
        }

        public BlogController(IFitnessRepository repository)
        {
            fitnessRepository = repository;
        }

        private BlogViewModel CreateBlogViewModel(string userName)
        {
            var b = fitnessRepository.GetBlogByUser(userName);
            if (b == null)
                return null;
            return new BlogViewModel(b);
        }

        //
        // GET: /Blog/        
        public ActionResult Index()
        {
            // TODO: Allow public profiles to show blog posts to anyone.
            if (User == null)
                return View("LogOn");

            var model = CreateBlogViewModel(User.Identity.Name);

            if (model == null)
                return RedirectToAction("Create", "Blog");

            return View(model);
        }

        //
        // GET: /Blog/posts/5
        [Authorize]
        public ActionResult Details(int id)
        {
            if (User == null)
                return View("LogOn");

            var post = fitnessRepository.GetBlogPost(id);            

            return View("SinglePost", post);
        }

        //
        // GET: /Blog/Create
        public ActionResult Create()
        {
            if (User == null)
                return View("LogOn");

            return View();
        } 

        // GET: /Blog/NewPost
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult NewPost()
        {
            if (User == null)
                return View("LogOn");

            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult NewPost(BlogPost p)
        {
            fitnessRepository.CreateBlogPost(p);

            return View("Index");
        }

        //
        // POST: /Blog/Create

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken(Salt = "saltyblog")]
        public ActionResult Create(Blog b)
        {
            if (User == null)
                return View("LogOn");

            try
            {
                // TODO: Add insert logic here
                var blog = b;
                blog.Author = fitnessRepository.GetUserByName(User.Identity.Name).UserId;

                fitnessRepository.CreateBlog(blog);
                fitnessRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Blog/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (User == null)
                return View("LogOn");

            var post = fitnessRepository.GetBlogPost(id);

            return View(post);
        }

        //
        // POST: /Blog/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (User == null)
                return View("LogOn");

            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
